//
//  CircleSegment.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleSegment.h"

@implementation CircleSegment
@synthesize _sprite = m_Sprite, _isDestroy = m_IsDestroy, _color = m_Color;

-(id) initWithType:(int)type color:(int)color rotation:(float)rotation
{
    if (self = [super init])
    {
        m_Type = type;
        m_Color = color;
        m_Rotation = rotation;
        m_IsDestroy = NO;
        
        [self createSprite];
        [self calcAnchorPoint];

        [m_Sprite setRotation:m_Rotation];
    }
    return self;
}

-(void) createSprite
{
    [self createSpriteWithColor];
    CGRect rec;
    switch (m_Type)
    {
        case SEGMENT_TYPE_FULL:
            break;
            
        case SEGMENT_TYPE_HALF:
            rec =  m_Sprite.textureRect;
            m_Sprite = [CCSprite spriteWithTexture:m_Sprite.texture rect:CGRectMake(rec.origin.x + rec.size.width / 2, rec.origin.y, rec.size.width / 2, rec.size.height)];
            break;
            
        case SEGMENT_TYPE_ONE_THIRD:
            break;
            
        case SEGMENT_TYPE_QUARTER:
            rec =  m_Sprite.textureRect;
            m_Sprite = [CCSprite spriteWithTexture:m_Sprite.texture rect:CGRectMake(rec.origin.x + rec.size.width / 2, rec.origin.y, rec.size.width / 2, rec.size.height / 2)];
            break;
            
        default:
            break;
    }
}

-(void) createSpriteWithColor
{
    switch (m_Color) {
        case SEGMENT_COLOR_BLUE:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_BLUE"];
            break;
            
        case SEGMENT_COLOR_GREEN:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_GREEN"];
            break;
            
        case SEGMENT_COLOR_RED:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_RED"];
            break;
            
        case SEGMENT_COLOR_YELLOW:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_YELLOW"];
            break;
            
        case SEGMENT_COLOR_PURPLE:
            m_Sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_CIRCLE_PURPLE"];
            break;
            
        default:
            break;
    }
}

-(void) calcAnchorPoint
{
    switch (m_Type)
    {
        case SEGMENT_TYPE_FULL:
            [m_Sprite setAnchorPoint:ccp(0.5f, 0.5f)];
            break;
            
        case SEGMENT_TYPE_HALF:
            [m_Sprite setAnchorPoint:ccp(0.0f, 0.5f)];
            break;
            
        case SEGMENT_TYPE_ONE_THIRD:
            [m_Sprite setAnchorPoint:ccp(0.5f, 0.0f)];
            break;
            
        case SEGMENT_TYPE_QUARTER:
            [m_Sprite setAnchorPoint:ccp(0.0f, 0.0f)];
            break;
            
        default:
            break;
    }
}

-(void) setPosition:(CGPoint)p
{
    [m_Sprite setPosition:p];
}

-(void) setRotation:(float)a
{
    [m_Sprite setRotation:(m_Rotation + a)];
}

-(void) setRadius:(float)r
{
    float ratio;
    if (m_Type == SEGMENT_TYPE_FULL) ratio = r / (m_Sprite.contentSize.width / 2);
    else ratio = r / m_Sprite.contentSize.width;
    [m_Sprite setScale:ratio];
}

-(void) addSpriteToBatch:(CCSpriteBatchNode *)batch
{
    [batch addChild:m_Sprite];
}

-(float) getMinAngle
{
    switch (m_Type)
    {
        case SEGMENT_TYPE_FULL:
            return 0.0f;
            
        case SEGMENT_TYPE_HALF:
            return 0.0f;
            
        case SEGMENT_TYPE_ONE_THIRD:
            return 0.0f;
            
        case SEGMENT_TYPE_QUARTER:
            return 0.0f;
            
        default:
            CCLOG(@"Something wrong");
            return 0.0f;
    }
}

-(void) appearWithTime:(ccTime)t
{
    float ratio = m_Sprite.scale;
    [m_Sprite setScale:0.0f];
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:t scale:ratio];
    CCEaseBackOut *bounceIn = [CCEaseBackOut actionWithAction:scaleTo];
    [m_Sprite runAction:bounceIn];
}

-(float) getMaxAngle
{
    switch (m_Type)
    {
        case SEGMENT_TYPE_FULL:
            return 360.0f;
            
        case SEGMENT_TYPE_HALF:
            return 180.0f;
            
        case SEGMENT_TYPE_ONE_THIRD:
            return 0.0f;
            
        case SEGMENT_TYPE_QUARTER:
            return 90.0f;
            
        default:
            CCLOG(@"Something wrong");
            return 0.0f;
    }
}

-(int) getColor
{
    return m_Color;
}

-(void) explodeWithDelay:(float)d
{
    CCDelayTime *delay = [CCDelayTime actionWithDuration:d];
    CCCallFunc *func = [CCCallFunc actionWithTarget:self selector:@selector(explodeEffect)];
    CCSequence *seq = [CCSequence actionOne:delay two:func];
    [m_Sprite runAction:seq];
    m_IsDestroy = YES;
}

-(void) explodeEffect
{
    m_Sprite.visible = NO;
}

-(void) dealloc
{
    [super dealloc];
}

@end
